//Handlebars.registerHelper('if_eq', function(v1, v2, opts) {
//	if(v1 == v2)
//		return opts.fn(this);
//	else
//		return opts.inverse(this);
//});

//导航
$('.navigation_n div').hover(function() {
	var index = $(this).index();
	if(index == 1) {
		$('.navigation_n div ul li').click(function(e) {
			e.stopPropagation();
			var index = $(this).index();
			if(index == 0) {
				window.location.href = './firststep.html'
			} else if(index == 1) {
				window.location.href = './specification.html'
			} else if(index == 2) {
				window.location.href = './trademark.html'
			}
		})
	} else if(index == 2) {
		$('.navigation_n div ul li').click(function(e) {
			e.stopPropagation();
			var index = $(this).index();
			if(index == 0) {
				window.location.href = '../indent/goodsindent.html'
			} else if(index == 2) {
				window.location.href = '../indent/evaluate.html'
			}
		})
	}
})

//
//获取选择的商品类别
var stairname = localStorage.getItem('stairname');
var stairid = localStorage.getItem('stairid');
var vfpname = localStorage.getItem('vfpname');
var vfpid = localStorage.getItem('vfpid');

$('.goodsSort').html(stairname + '>' + vfpname);


//获取规格
var sku = $('#sku').html();
var skuPages = Handlebars.compile(sku);
var skuu = $('#skuu').html();
var skuuPages = Handlebars.compile(skuu);
var id = '';
var valuename = '';
var idd=[];
var property = [];
var skuname_n = [];
$.ajax({
	type:'post',
	url:URL.interface.getCategoryProperty,
	data:{
		categoryFirstId:stairid,
		categorySecondId:vfpid
	},
	success:function(e){
		console.log(e.property)
		if(e.code == '10000'){
			$("#skuL").html(skuPages(e.property));
			var num = 0
		  	$('#skuL input').click(function(){
		  		id = $(this).attr("id");
		  		valuename = $(this).attr("value");
		  		if(num < 1){
				  	num ++
				  	$(this).is(':checked');
				  	$(this).attr("disabled","disabled");
			  	}else{
			  		$('#skuL input').attr("disabled","disabled");
			  	}
			  	
			  	var propertyid = [];
			  	for(i in e.property){
			  		propertyid.push(e.property[i].id);
			  	}
			  	property.push(e.property[(propertyid).indexOf(id)]);
			  	
			  	$("#skuLL").html(skuuPages(property));
			  	//点击获取规格填写
				$('.standardValue_d input').on("click",function(){
				  	skuname_n.push($(this).attr("value"));
				  	var dataid = $(this).attr("id");
				  	idd.push(dataid);
				  	if($(this).is(':checked') == true){
				  		var html = '';
				  		for(i in skuname_n){
				  			html += '<tr><td class="skuname_n">'+skuname_n[i]+'</td><td></td><td><input type="text"   class="supplyPrice"/></td><td><input type="text" class="inventTorys"/></td><td><input type="text" class="num"/></td></tr>'
				  		}
				  		$('.goods_table_s').html(html);
				  		$('.skuname').html($('.specification').html());
				  	}else if($(this).is(':checked') == false){
				  		skuname_n.splice($.inArray($(this).attr("value"),skuname_n),1);
				  		skuname_n.splice($.inArray($(this).attr("value"),skuname_n),1)
				  		var html = '';
				  		for(i in skuname_n){
				  			html += '<tr><td class="skuname_n">'+skuname_n[i]+'</td><td></td><td><input class="supplyPrice" type="text"/></td><td><input type="text" class="inventTorys"/></td><td><input type="text" class="num"/></td></tr>'
				  		}
				  		$('.goods_table_s').html(html);
				  	}
					
				})
					
			})
		  			
		  	
		}
		
	}
})


//发布商品
$('.nextStep').click(function(){
		var goodename=[];
		var supplyPrice = [];
		var inventTorys = [];
		var num = [];
		for(var i=0;i<$('.skuname_n').length;i++){
			goodename.push($('.skuname_n').eq(i).text());
		}
		
		for(var i=0;i<$('.supplyPrice').length;i++){
			supplyPrice.push($('.supplyPrice').eq(i).val());
		}
		
		for(var i=0;i<$('.inventTorys').length;i++){
			inventTorys.push($('.inventTorys').eq(i).val());
		}
		
		for(var i=0;i<$('.num').length;i++){
			num.push($('.num').eq(i).val());
		}
		
		
		var list = [];
		for(var i=0;i<goodename.length;i++){
				list.push({"firstProName":valuename,"firstProNameId":id,"firstProvalue":goodename[i],"firstProValueId":idd[i],"secoundProName":"","secoundProNameId":"","secoundProvalue":"","secoundProValueId":"","supplyPriceStr":supplyPrice[i],"businessNoStr":num[i],"inventTorysStr":inventTorys[i]})
		}
		$.ajax({
			url:URL.interface.addgoods,
			type:'post',
			dataType:'json',
			data:{
				goodsSkus:JSON.stringify(list),
				goodsName:$('.goodsName').val(),
				goodsBrand:$('.goodsBrand').val(),
				goodsMainImg:$(".goodsMainImg").attr("src"),
				goodsBanners:String(badderimg),
				goodsDetailImg:String(particularimg),
				categoryId:String(vfpid)
			},
			success:function(e){
				console.log(e)
			}
		})
})